//
//  CCComCellViewModel.m
//  Activity
//
//  Created by its on 15/5/22.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCComCellViewModel.h"

@interface CCComCellViewModel ()
@property (nonatomic, strong) CCComment *comment;
@property (nonatomic, copy) void(^block)(NSString *objectid, NSString *name, NSString *author);
@end

@implementation CCComCellViewModel

- (RACSignal *) upAction {
  return [self.comment up];
}
- (RACSignal *) reply {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    _block(self.comment.objectid, self.comment.author, self.comment.authorid);
    [subscriber sendCompleted];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
  }];
}
- (id) initWithComment:(CCComment *) comment commentBlock:(void(^)(NSString *id, NSString *name, NSString *author))block{
  if ( self = [super init] ) {
    _comment = comment;
    _block = [block copy];
    @weakify(self);
    [RACObserve(self.comment, author) subscribeNext:^(id x) {
      @strongify(self);
      self.author = [[NSAttributedString alloc] initWithString:x attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                             NSForegroundColorAttributeName:[UIColor blackColor]}];
    }];
    [RACObserve(self.comment, upCount)  subscribeNext:^(NSNumber *x) {
      @strongify(self);
      self.upCount = [[NSAttributedString alloc] initWithString:x.description attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                              NSForegroundColorAttributeName:[UIColor blackColor]}];
    }];
    [[RACSignal combineLatest:@[RACObserve(self.comment, commentAuthor), RACObserve(self.comment, comment)] reduce:^id(NSString *x, NSString *y){
      if (x == nil) {
        return @[y];
      } else {
        return @[x, y];
      }
    }] subscribeNext:^(NSArray *x) {
          @strongify(self);
      if (x.count == 1) {
        self.content = [[NSMutableAttributedString alloc] initWithString:x[0]
                                                              attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                           NSForegroundColorAttributeName:[UIColor blackColor],
                                                                           }];
      } else {
        self.content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"@%@:%@",x[0],x[1]]];
        [self.content addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                      NSForegroundColorAttributeName:[UIColor grayColor],
                                      NSLinkAttributeName:x[0]}
                              range:NSMakeRange(1, [(NSString *)x[0] length])];
        [self.content addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                      NSForegroundColorAttributeName:[UIColor blackColor],
                                      }
                              range:[self.content.string rangeOfString:x[1]]];
      }

    }];
    [RACObserve(self.comment, head) subscribeNext:^(id x) {
      @strongify(self);
      self.head = [NSURL URLWithString:x];
    }];
  }
  return self;
}

- (void) dealloc {
  NSLog(@"cell view model dealloc");
}
@end
